package com.moly.virgo.util;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.DateUtil;
import com.aliyun.oss.common.utils.HttpHeaders;
import com.aliyun.oss.model.*;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import java.io.File;
import java.net.URL;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class STSUtil {

    public static void main(String[] args) {

//        createBucket();
//        String s=getToken();
//        System.out.println(s);
//        test11();
        test11();
    }


    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static final String accessKeySecret = "neLKvvgXPoFQkuvejkAxQVS3VSw66M";
    private static String accessKeyId = "LTAIWr08O0pCBh6n";
    private static String securityToken = "";
    private static String bucketName = "molyxbb4";
    private static String objectName =
            "store/076499eb-9e60-11e9-99eb-0242ac110002.jpg";

    private static void upload() {

    }

    private static Map<String, String> getToken() {

        String endpoint = "sts.aliyuncs.com";
        String accessKeyId = "LTAIWr08O0pCBh6n";
        String accessKeySecret = "neLKvvgXPoFQkuvejkAxQVS3VSw66M";
        String roleArn = "acs:ram::1474099693574362:role/xbbuser";
        roleArn = "acs:ram::1474099693574362:role/role-testcros";
        String roleSessionName = "session-name";
        String policy = "{\n" +
                "    \"Version\": \"1\", \n" +
                "    \"Statement\": [\n" +
                "        {\n" +
                "            \"Action\": [\n" +
                "                \"oss:*\"\n" +
                "            ], \n" +
                "            \"Resource\": [\n" +
                "                \"acs:oss:*:*:*\" \n" +
                "            ], \n" +
                "            \"Effect\": \"Allow\"\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        try {
            // 添加endpoint（直接使用STS endpoint，前两个参数留空，无需添加region ID）
            DefaultProfile.addEndpoint("", "", "Sts", endpoint);
            // 构造default profile（参数留空，无需添加region ID）
            IClientProfile profile = DefaultProfile.getProfile("", accessKeyId, accessKeySecret);
            // 用profile构造client
            DefaultAcsClient client = new DefaultAcsClient(profile);
            final AssumeRoleRequest request = new AssumeRoleRequest();
            request.setMethod(MethodType.POST);
            request.setRoleArn(roleArn);
            request.setRoleSessionName(roleSessionName);
            request.setPolicy(policy); // 若policy为空，则用户将获得该角色下所有权限
            request.setDurationSeconds(1000L); // 设置凭证有效时间
            final AssumeRoleResponse response = client.getAcsResponse(request);
            System.out.println("Expiration: " + response.getCredentials().getExpiration());
            System.out.println("Access Key Id: " + response.getCredentials().getAccessKeyId());
            System.out.println("Access Key Secret: " + response.getCredentials().getAccessKeySecret());
            System.out.println("Security Token: " + response.getCredentials().getSecurityToken());

            System.out.println("RequestId: " + response.getRequestId());
            HashMap<String, String> map = new HashMap<>();
            map.put("keyid", response.getCredentials().getAccessKeyId());
            map.put("secret", response.getCredentials().getAccessKeySecret());
            map.put("token", response.getCredentials().getSecurityToken());
            return map;
        } catch (ClientException e) {
            System.out.println("Failed：");
            System.out.println("Error code: " + e.getErrCode());
            System.out.println("Error message: " + e.getErrMsg());
            System.out.println("RequestId: " + e.getRequestId());
        }
        return null;

    }


    private static void test11() {

        Map<String, String> map = getToken();
        // keyid   secret
        String keyid = map.get("keyid");
        String secret = map.get("secret");
        String token = map.get("token");
        System.out.println(securityToken);
        if (keyid == "" || secret == "") {
            return;
        }
        // 以杭州为例
        // 创建OSSClient实例。
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        OSSClient client = new OSSClient(endpoint, keyid, secret, token);
        String bucketName = "molyxbb3";
        bucketName="testcros";
        String molyxbb4 = client.getBucketLocation(bucketName);
        System.out.println("molyxbb4  ="+molyxbb4);
        AccessControlList bucketAcl = client.getBucketAcl(bucketName);
        System.out.println(bucketAcl.toString());

        PutObjectResult ccc = client.putObject(bucketName, "ccc", new File("D:\\moly\\test.json"));
        System.out.println(ccc.toString());
        System.out.println(ccc.toString());
        client.shutdown();


    }
}
